package org.tinygroup.dbf.impl;

import org.tinygroup.dbf.DbfField;
import org.tinygroup.dbf.Util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by luoguo on 2014/7/10.
 */
public final class Utils {
    private static final SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMdd");

    private Utils() {
    }

    public static byte[] getBinary(Object value, DbfField field, String encode) throws UnsupportedEncodingException, IOException {
        byte[] result = new byte[0];
        char type = field.getType();
        switch (type) {
            case 'c':
            case 'C':
                result = value.toString().getBytes(encode);
                break;
            case 'd':
            case 'D':
            	if(value!=null) result = new SimpleDateFormat("yyyyMMdd").format(((Date)value)).getBytes(encode);
                break;
            case 'n':
            case 'N':
                if(field.getDecimal()==0) {
                    result = String.valueOf((Long) value).getBytes();
                }else{
                    result = String.valueOf((Double) value).getBytes();
                }
                break;
            case 'f':
            case 'F':
                result = String.valueOf((Float) value).getBytes();
                break;
            case 'o':
            case 'O':
                result = String.valueOf((Double) value).getBytes();
                break;
            case 'b':
            case 'B':
                result = String.valueOf((Double) value).getBytes();
                break;
            case 'l':
            case 'L':
                result = Util.getByteFromBoolean((Boolean) value);
                break;
            default:
                throw new IOException("不支持的类型：" + type + ",值为：" + value.toString());
        }

        return result;
    }


    public static Object getValue(String value, DbfField field) throws IOException {
        if (value == null) {
            return null;
        }
        char type = field.getType();
        if (type == 'c' || type == 'C') {
            return value;
        } else if (type == 'n' || type == 'N') {
            if (field.getDecimal() == 0) {
                return getLongValue(value);
            }
            return getDoubleValue(value);
        } else if (type == 'i' || type == 'I') {
            return getLongValue(value);
        } else if (type == 'f' || type == 'F') {
            return getFloatValue(value);
        } else if (type == 'o' || type == 'O') {
            return getDoubleValue(value);
        } else if (type == 'd' || type == 'D') {
            return getDateValue(value);
        } else if (type == 'l' || type == 'L') {
            return getBooleanValue(value);
        }
        throw new IOException("不支持的类型：" + type + ",值为：" + value);
    }

    static Long getLongValue(String value) throws UnsupportedEncodingException {
        if (value.equals("-") || value.equals("")) {
            return null;
        }
        return Long.parseLong(value);
    }

    static Double getDoubleValue(String value) throws UnsupportedEncodingException {
        if (value.equals("-") || value.equals("")) {
            return null;
        }
        return Double.parseDouble(value);
    }

    static Float getFloatValue(String value) throws UnsupportedEncodingException {
        if (value.equals("-") || value.equals("")) {
            return null;
        }
        return Float.parseFloat(value);
    }

    static Boolean getBooleanValue(String value) throws UnsupportedEncodingException {
        return Util.getBooleanValue(value);
    }

    static Date getDateValue(String value) throws UnsupportedEncodingException {
        try {
            if (value.equals("")) {
                return null;
            }
            return dataFormat.parse(value);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
